package com.oa.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oa.bean.AppUserDept;
import com.oa.bean.Appraise;
import com.oa.bean.Contract;
import com.oa.service.AMService;
import com.oa.service.impl.AMServiceImpl;

@Controller
@RequestMapping("/showApp")
public class AppraiseManage {
	
	@Autowired
	private AMServiceImpl aMService;

	/**
	 * 添加考核信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/addApp")
	public String addCon(HttpServletRequest request){
		String ar_appuser=request.getParameter("user");
		String ar_num=request.getParameter("number");
		String ar_name=request.getParameter("ar_name");
		int ar_dept_id=Integer.parseInt(request.getParameter("type"));
		int ar_status=Integer.parseInt(request.getParameter("ckey"));
		String ar_issdate=request.getParameter("fbdate");
		String ar_appdate=request.getParameter("testdate");
		String ar_enclo=request.getParameter("appendix");
		String ar_remaks=request.getParameter("replyContent");
		
		Appraise appraise=new Appraise();
		appraise.setAr_appuser(ar_appuser);
		appraise.setAr_appdate(ar_appdate);
		appraise.setAr_dept_id(ar_dept_id);
		appraise.setAr_enclo(ar_enclo);
		appraise.setAr_issdate(ar_issdate);
		appraise.setAr_name(ar_name);
		appraise.setAr_remaks(ar_remaks);
		appraise.setAr_status(ar_status);
		appraise.setAr_num(ar_num);
		
		int result=aMService.addApp(appraise);
		
		
		return "../Template/showAppraise";
	}
	
	/**
	 * 显示考核列表
	 * @param currentPage
	 * @param appuser
	 * @param dept
	 * @param status
	 * @param issdate
	 * @param appdate
	 * @return
	 */
	@RequestMapping("/findAppraise")
	@ResponseBody
	public Map<String, Object> findAppraise(Integer currentPage,String appuser,Integer dept,Integer status,String issdate,String appdate){
		Appraise appraise=new Appraise();
		appraise.setAr_appuser(appuser);
		appraise.setAr_appdate(appdate);
		appraise.setAr_dept_id(dept);
		appraise.setAr_issdate(issdate);
		appraise.setAr_status(status);
		System.out.println(appraise);
		
		if(currentPage==0){
			currentPage=1;
		}
		System.out.println("currentPage="+currentPage);
		Page<PageInfo> pageInfo=PageHelper.startPage(currentPage, 6,"ar_id desc");
		List<AppUserDept> appUserDepts=aMService.findAppraise(appraise);
		System.out.println(appUserDepts);
		Map<String,  Object> map =new HashMap<>();
		map.put("appUserDepts", appUserDepts);
		map.put("pages", pageInfo.getPages());
		return map;
	}
	
	/**
	 * 根据编号查询考核信息
	 * @param ar_num
	 * @return
	 */
	@RequestMapping("/findApp")
	@ResponseBody
	public Map<String, Object> findApp(String ar_num){
		AppUserDept appUserDept=aMService.findApp(ar_num);
		System.out.println(appUserDept);
		Map<String,  Object> map =new HashMap<>();
		map.put("appUserDept", appUserDept);
		return map;
	}
	
	/**
	 * 根据id查找考核信息
	 * @param ar_id
	 * @param model
	 * @return
	 */
	@RequestMapping("/findApp1")
	public String findApp1(int ar_id,Model model){
		AppUserDept appUserDept=aMService.findApp1(ar_id);
		System.out.println(appUserDept);
		model.addAttribute("appUserDept", appUserDept);
		return "../Template/updateApp";
	}
	
	/**
	 * 根据id修改考核信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateApp")
	public String updateApp(HttpServletRequest request){
		int ar_id=Integer.parseInt(request.getParameter("ar_id"));
		String ar_appuser=request.getParameter("user");
		String ar_num=request.getParameter("number");
		String ar_name=request.getParameter("ar_name");
		int ar_dept_id=Integer.parseInt(request.getParameter("type"));
		int ar_status=Integer.parseInt(request.getParameter("ckey"));
		String ar_issdate=request.getParameter("signdate");
		String ar_appdate=request.getParameter("testdate");
		String ar_enclo=request.getParameter("appendix");
		double ar_percent=Double.parseDouble(request.getParameter("percent"));
		String ar_remaks=request.getParameter("replyContent");
		
		Appraise appraise=new Appraise();
		appraise.setAr_id(ar_id);
		appraise.setAr_appuser(ar_appuser);
		appraise.setAr_num(ar_num);
		appraise.setAr_name(ar_name);
		appraise.setAr_dept_id(ar_dept_id);
		appraise.setAr_status(ar_status);
		appraise.setAr_issdate(ar_issdate);
		appraise.setAr_appdate(ar_appdate);
		appraise.setAr_enclo(ar_enclo);
		appraise.setAr_percent(ar_percent);
		appraise.setAr_remaks(ar_remaks);
		
		int result=aMService.updateApp(appraise);
		System.out.println(result);
		
		return "../Template/showAppraise";
	}
	
	/**
	 * 批量删除
	 * @param str
	 * @return
	 */
	@RequestMapping("/deleteMore")
	@ResponseBody
	public Map<String, Object> deleteMore(String str){
		String[] ptids=str.split(",");
		System.out.println(ptids);
		Map<String, Object> map=new HashMap<>();
		for (int i = 0; i < ptids.length; i++) {
			int ar_id=Integer.parseInt(ptids[i]);
			int result=aMService.deleteApp(ar_id);
			if (result!=0) {
				map.put("res", result);
			}
		}
		return map;
	}
}
